/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>' + d[i].wh_name + '</td>';
        h += '<td>';
        h += d[i].starthour + '<br>-<br>' + d[i].endhour;
        h += '<td>' + d[i].staff_name + '</td>';
        h += '<td>' + d[i].status_name + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="window.location.href=\'wh-edit.html?id=' + d[i].id + '\';">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//选项
function getOne(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/one',
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            console.log(res.data);
            oneHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//详情HTML
function oneHtml(e){
    var d = e.data;
    var itemList = e.item;
    var taskList = e.task;
    var h, i;
    //项目
    h = '';
    if (d.item_id == 0) {
        h += '<option value="0" selected>-选择项目-</option>';
    } else {
        h += '<option value="0">-选择项目-</option>';
    }
    for (i = 0; i < itemList.length; i ++) {
        if (d.item_id == itemList[i].id) {
            h += '<option value="' + itemList[i].id + '" selected>';
        } else {
            h += '<option value="' + itemList[i].id + '">';
        }
        h += itemList[i].title;
        h += '</option>';
    }
    $('#item_id').html(h);
    //任务
    h = '';
    if (d.task_id == 0) {
        h += '<option value="0" selected>-选择任务-</option>';
    } else {
        h += '<option value="0">-选择任务-</option>';
    }
    for (i = 0; i < taskList.length; i ++) {
        if (d.task_id == taskList[i].id) {
            h += '<option value="' + taskList[i].id + '" selected>';
        } else {
            h += '<option value="' + taskList[i].id + '">';
        }
        h += taskList[i].title;
        h += '</option>';
    }
    $('#task_id').html(h);
    //工时项
    h = '';
    var wh = e.wh, cwh = d.whList, hours = e.hours; //选项，已有的，时间
    var ii, iii, child;
    for (i = 0; i < cwh.length; i ++) {
        h += '<style type="text/css">.formGroup { margin: 10px 3vw; border: 1px solid #eee; clear: both; } .formBox { margin: 20px; height: 200px; border: 1px solid #eee; clear: both; } .gsSub { padding: 0 10px; padding-top: 5px; color: #0088CC; float: right; } .wh-child { width: 85%; }</style>';
        h += '<div class="formGroup">';
        h += '<div class="row cl">';
        h += '<label class="form-label col-xs-4 col-sm-2">';
        h += '父级工时：';
        h += '</label>';
        h += '<div class="formControls col-xs-8 col-sm-9">';
        h += '<select class="input-text wh-child" id="wh_id">';
        if (cwh[i].config_whid == 0) {
            h += '<option value="" selected>-选择父级-</option>';
        } else {
            h += '<option value="">-选择父级-</option>';
        }
        for  (ii = 0; ii < wh.length; ii ++) {
            if (cwh[i].config_whid == wh[ii].id) {
                h += '<option value="' + wh[ii].id + '" selected>';
            } else {
                h += '<option value="' + wh[ii].id + '">';
            }
            h += wh[ii].title;
            h += '</option>';
        }
        h += '</select>';
        h += '<a href="javascript:;" class="gsSub" onclick="itemAddHtml();">新增子级</a>';
        h += '</div>';
        h += '</div>';
        child = cwh[i].child;
        for (ii = 0; ii < child.length; ii ++) {
            h += '<div class="formBox">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '子级工时：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" placeholder="输入子级名称" id="sub_title' + child[ii].id + '" value="' + child[ii].title + '"/>';
            h += '<a href="javascript:;" class="gsSub" onclick="itemModify(' + child[ii].id + ');">再次保存</a>';
            h += '<input type="hidden" id="wh_id' + child[ii].id + '" value="' + id + '"/>';
            h += '<input type="hidden" id="config_whid' + child[ii].id + '" value="' + cwh[i].config_whid + '"/>';
            //h += '<input type="hidden" id="title' + child[ii].id + '" value="' + child[ii].title + '"/>';
            //h += '<input type="hidden" id="start' + child[ii].id + '" value="' + child[ii].start + '"/>';
            //h += '<input type="hidden" id="end' + child[ii].id + '" value="' + child[ii].end + '"/>';
            //h += '<input type="hidden" id="delay' + child[ii].id + '" value="' + child[ii].delay + '"/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '开始时间段：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<select class="input-text wh-child" id="starthour' + child[ii].id + '" onchange="getDelay(' + child[ii].id + ');">';
            if (child[ii].start == '') {
                h += '<option value="" selected>-选择时间-</option>';
            } else {
                h += '<option value="">-选择时间-</option>';
            }
            for  (iii = 0; iii < hours.length; iii ++) {
                if (child[ii].start == hours[iii]) {
                    h += '<option value="' + hours[iii] + '" selected>';
                } else {
                    h += '<option value="' + hours[iii] + '">';
                }
                h += hours[iii];
                h += '</option>';
            }
            h += '</select>';
            //h += '<a href="javascript:;" class="gsSub" style="color: red;" onclick="">删除</a>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '结束时间段：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<select class="input-text wh-child" id="endhour' + child[ii].id + '" onchange="getDelay(' + child[ii].id + ');">';
            if (child[ii].end == '') {
                h += '<option value="" selected>-选择时间-</option>';
            } else {
                h += '<option value="">-选择时间-</option>';
            }
            for  (iii = 0; iii < hours.length; iii ++) {
                if (child[ii].end == hours[iii]) {
                    h += '<option value="' + hours[iii] + '" selected>';
                } else {
                    h += '<option value="' + hours[iii] + '">';
                }
                h += hours[iii];
                h += '</option>';
            }
            h += '</select>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '时长：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" id="delay' + child[ii].id + '" disabled value="' + child[ii].delay + '"/>';
            h += '</div>';
            h += '</div>';
            h += '</div>';
        }
        h += '';
        h += '<span id="whAdd"></span>';
        h += '</div>';
    }
    $('#formEdit').html(h);
}


//新增子级
function itemAddHtml(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/one',
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
        },
        error: function () {
            console.log('网络错误');
        }
    });
    var h = '';
    h += '<div class="formBox">';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '子级工时：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text wh-child" placeholder="输入子级名称" id="sub_title"/>';
    h += '<a href="javascript:;" class="gsSub" onclick="">保存</a>';
    h += '</div>';
    h += '</div>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '开始时间段：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<select class="input-text wh-child" id="starthour" onchange="getDelay();">';
    //if (child[ii].start == '') {
    //    h += '<option value="" selected>-选择父级-</option>';
    //} else {
    //    h += '<option value="">-选择父级-</option>';
    //}
    //for  (iii = 0; iii < hours.length; iii ++) {
    //    if (child[ii].start == hours[iii]) {
    //        h += '<option value="' + hours[iii] + '" selected>';
    //    } else {
    //        h += '<option value="' + hours[iii] + '">';
    //    }
    //    h += hours[iii];
    //    h += '</option>';
    //}
    h += '</select>';
    //h += '<a href="javascript:;" class="gsSub" style="color: red;" onclick="">删除</a>';
    h += '</div>';
    h += '</div>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '结束时间段：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<select class="input-text wh-child" id="endhour" onchange="getDelay(\'\');">';
    //if (child[ii].end == '') {
    //    h += '<option value="" selected>-选择父级-</option>';
    //} else {
    //    h += '<option value="">-选择父级-</option>';
    //}
    //for  (iii = 0; iii < hours.length; iii ++) {
    //    if (child[ii].end == hours[iii]) {
    //        h += '<option value="' + hours[iii] + '" selected>';
    //    } else {
    //        h += '<option value="' + hours[iii] + '">';
    //    }
    //    h += hours[iii];
    //    h += '</option>';
    //}
    h += '</select>';
    h += '</div>';
    h += '</div>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '时长：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text wh-child" id="delay" disabled/>';
    h += '</div>';
    h += '</div>';
    h += '</div>';
    $('#whAdd').html(h);
}


//设置项目
function setByItem(){
    var uinfo = getAdminInfo();
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    var formData = {
        id: id, item_id: item_id, task_id: task_id, uid: uinfo.uid,
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/setbyitem',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //window.location.reload();
            window.location.href = 'wh-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//工时表单
function formAdd(){
    var h = '', i;
    h += '<style type="text/css">.formGroup { margin: 10px 3vw; border: 1px solid #eee; clear: both; } .formBox { margin: 20px; height: 200px; border: 1px solid #eee; clear: both; } .gsSub { padding: 0 10px; padding-top: 5px; color: #0088CC; float: right; }</style>';
    h += '<div class="formGroup">';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '父级工时：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<select class="input-text" id="wh_id">';
    h += '<option value="">-选择父级-</option>';
    h += '</select>';
    h += '</div>';
    h += '</div>';
    h += '<div class="formBox">';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '子级工时：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入子级名称" id="sub_title" style="width: 90%;"/>';
    h += '<a href="javascript:;" class="gsSub" onclick="itemAdd();">保存</a>';
    h += '</div>';
    h += '</div>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '开始时间段：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<select class="input-text" id="starthour" style="width: 90%;" onchange="getDelay();"></select>';
    h += '<a href="javascript:;" class="gsSub" style="color: red;" onclick="">删除</a>';
    h += '</div>';
    h += '</div>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '结束时间段：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<select class="input-text" id="endhour" style="width: 90%;" onchange="getDelay();"></select>';
    h += '</div>';
    h += '</div>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2">';
    h += '时长：';
    h += '</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" id="delay" style="width: 90%;" disabled/>';
    h += '</div>';
    h += '</div>';
    h += '</div>';
    h += '';
    h += '</div>';
    $('#formAdd').html(h);
    $('#addBtn').hide();
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/config-wh/index',
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var wh = res.data.wh_id;
            var hour = res.data.hour;
            //父级工时
            h = '';
            h += '<option value="">-选择父级工时-</option>';
            for (i = 0; i < wh.length; i ++) {
                h += '<option value="' + wh[i].id + '">';
                h += wh[i].title;
                h += '</option>';
            }
            $('#wh_id').html(h);
            //时间
            h = '';
            h += '<option value="">-选择时间-</option>';
            for (i = 0; i < hour.length; i ++) {
                h += '<option value="' + hour[i] + '">';
                h += hour[i];
                h += '</option>';
            }
            $('#starthour').html(h);
            $('#endhour').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//计算时长
function getDelay(v){
    var starthour = $('#starthour' + v).val();
    var endhour = $('#endhour' + v).val();
    if (starthour == '' || endhour == '') {
        return;
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/delay-get',
        data: { start: starthour, end: endhour },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            $('#delay' + v)[0].value = res.data.str;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//设置子工时
function itemAdd(){
    var wh_id = $('#wh_id').val();
    var wh_title = $('#wh_title').val();
    var starthour = $('#starthour').val();
    var endhour = $('#endhour').val();
    var formData = {
        id: id, wh_id: wh_id, wh_title: wh_title,
        starthour: starthour, endhour: endhour,
    };
    if (formData.wh_id == 0) {
        layerMsgErr('父级工时必须的'); return
    }
    if (formData.wh_title == '') {
        layerMsgErr('子级工时必须的'); return
    }
    if (formData.starthour == 0) {
        layerMsgErr('开始时间必须的'); return
    }
    if (formData.starthour == 0) {
        layerMsgErr('开始时间必须的'); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/add',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: res.msg});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改子工时
function itemModify(v){
    var wh_id = $('#wh_id' + v).val();
    var config_whid = $('#config_whid' + v).val();
    var title = $('#title' + v).val();
    var start = $('#start' + v).val();
    var end = $('#end' + v).val();
    var delay = $('#delay' + v).val();
    console.log(config_whid,title,start,end,delay);
    var formData = {
        id: v, wh_id: wh_id,
        config_whid: config_whid,
        title: title, start: start, end: end,
        delay: delay,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: res.msg});
            setTimeout(function(){
                window.location.reload();
            }, 500);
            //window.location.href = 'wh-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


////计算工时
//function jisuan(){
//    var start = $('#start').val();
//    var end = $('#end').val();
//    if (start == '') { return }
//    if (end == '') { return }
//    if (start > end) { return }
//    var formData = {
//        id: id, start: start, end: end,
//    };
//    $.ajax({
//        type: apiMethod,
//        url: apiDomain + 'wh/jisuan',
//        data: formData,
//        success: function (res) {
//            console.log(res);
//            if (res.code < 0) {
//                msgPop({msg: res.msg});
//                return
//            }
//            $('#gongshi').html(res.data.str);
//            $('#delay')[0].value = res.data.fen;
//        },
//        error: function () {
//            console.log('网络错误');
//        }
//    });
//}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//列表
function authIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(authHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function authHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>' + d[i].wh_name + '</td>';
        h += '<td>';
        h += d[i].starthour + '<br>-<br>' + d[i].endhour;
        h += '<td>' + d[i].staff_name + '</td>';
        h += '<td>' + d[i].status_name + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_ok(' + d[i].id + ');" style="color: green;">';
        h += '<b>通过</b>';
        h += '</a>';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_no(\'审核拒绝\',\'wh-auth-no.html\', ' + d[i].id + ');" style="color: red;">';
        h += '<b>拒绝</b>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//审核通过
function lay_auth_ok(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/auth-ok',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已通过!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//审核拒绝弹窗
function lay_auth_no(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#item_id' + id).val();
    //tt = tt ? escape(tt) : '';
    //url += '&item_id=' + tt;
    h = 350;
    layer_show(title,url,w,h);
}


//审核拒绝
function auth_no() {
    var remark = $('#remark').val();
    if (!remark) {
        msgPop({msg: '缘由必须的'}); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-no',
        data: {  id: id, remark: remark },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('已拒绝!', {icon: 1, time: 1000});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}